<template>
	<view class="aiWriting_box">
		<view class="content">
			<view class="textarea">
				<u-textarea height="722rpx" maxlength="-1" v-model="txt.value" border="false"
					placeholder="点击输入文案或长按粘贴文案">
				</u-textarea>
			</view>
			<view class="tools">
				<view class="tools_left">
					<view class="left" @click="delTxt">
						<u-icon name="trash" size="18" color="#999999"></u-icon>
						<text class="text">清除文本</text>
					</view>
				</view>
				<view class="tools_right">
					<text class="text">字数 {{txt.value.length}}</text>
				</view>
			</view>
		</view>
		<view class="button">
			<view class="button_left">
				<u-button text="开始提取" color="#5490FF" size="large" @click="getKeyWords"></u-button>
			</view>
			<view class="button_right">
				<u-button text="复制关键词" color="#1EB930" size="large" @click="copyResults"></u-button>
			</view>
		</view>
		<view class="creative_results" v-if="results.length">
			<view class="title">
				提取结果
			</view>
			<view class="results_keyWords">
				<ul>
					<li v-for="(item,index) in results" :key="index">
						<view class="">
							{{item.word}}
						</view>
						<view class="">
							{{(item.score*100).toFixed(2)}}%
						</view>
					</li>
				</ul>
			</view>
			<!-- <view class="title">
				提取结果
			</view>
			<view class="results_keyWords">
				{{results}}
			</view> -->
			<!-- <view class="creative_results_btn" @click="copyResults">
        <u-button text="复制结果" color="#5490FF" size="large"></u-button>
      </view> -->
		</view>
		<view style="width: 100%; height: 40rpx;background-color: #f4f4f4;"></view>
		<!-- 非会员看广告提示 -->
		<u-modal :show="vipShow" :title="'温馨提示'" :content="content" cancelText="取消" confirmText="去开通"
			showCancelButton="true" @cancel="cancel" @confirm="govip" :closeOnClickOverlay="true"
			@close="vipShow = false"></u-modal>
	</view>
</template>

<script>
	// var nodejieba = require("nodejieba-master");
	import {
		wxTextDetection
	} from "../../utils/textCheck.js";
	import {
		wycUrl
	} from '@/api/assemb.js'
	export default {
		data() {
			return {
				vipShow: false,
				isVip: false,
				content: '您还不是会员,此功能需开通会员才可使用。',
				txt: {
					value: ''
				},
				results: [],
				accessToken: ''
			};
		},
		onLoad() {
			this.accessToken = getApp().globalData.accessToken
		},
		onShow() {
			this.isVip = uni.getStorageSync('vipType').vipStatus == 1 ? true : false;
			// this.isVip = true;
		},
		methods: {
			getKeyWords() {
				if (this.txt.value.trim() == '') {
					uni.showToast({
						title: '请输入文案',
						duration: 2000,
						icon: 'none'
					});
				} else if (this.isVip) {
					wxTextDetection(this.txt.value).then((res) => {
						if (res.isItPass) {
							console.log('关键词提取');
							uni.showLoading({
								title: '加载中...',
								mask: true
							})
							wx.request({
								url: `https://aip.baidubce.com/rpc/2.0/nlp/v1/txt_keywords_extraction?access_token=${this.accessToken}&charset=UTF-8`,
								method: "POST",
								data: {
									text: [this.txt.value],
									num: 20
								},
								success: (res) => {
									console.log("res>>", res);
									if (!res.data.error_code) {
										this.results = res.data.results
										uni.hideLoading()
									} else {
										uni.showToast({
											title: res.data.error_msg,
											icon: 'error',
											duration: 2000
										});
									}

								}
							})
							// let param = {
							// 	"content": `请提取以下文字的关键词:'${this.txt.value}'`
							// }
							// wycUrl(param, 1005, 'chatAzure2').then((res) => {
							// 	console.log(res);
							// 	this.results = res.data.data.results[0].replace(/[\r\n]/g,
							// 		'')
							// 	uni.hideLoading()
							// 	console.log(this.results);
							// })
						}
					})

				} else {
					this.vipShow = true
				}
			},
			copyResults() {
				if (this.results.length != 0) {
					var txtRes = ''
					this.results.forEach((item) => {
						txtRes = txtRes + item.word + ','
					})
					txtRes = txtRes.slice(0, txtRes.length - 1)
					uni.setClipboardData({
						data: txtRes,
						showToast: true
					});
				} else {
					uni.showToast({
						title: '请先提取关键词',
						icon: 'none',
						duration: 2000
					})
				}
			},
			cancel() {
				this.vipShow = false
			},
			govip() {
				this.vipShow = false;
				if (uni.getStorageSync('OS') !== 'ios') {
					uni.navigateTo({
						url: '/pages/paymentCenter/paymentCenter'
					});
				} else {
					if (getApp().globalData.iosPay == 0) {
						uni.showModal({
							title: '提示',
							content: '由于相关规范，IOS功能暂不可用',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					} else if (getApp().globalData.iosPay == 1) {
						uni.navigateTo({
							url: '/pages/goods/goods'
						});
					}
				}
			},
			delTxt() {
				this.txt.value = ''
				this.results = []
			}
		}
	}
</script>
<style lang="scss">
	/deep/.u-textarea--disabled {
		background-color: #fff !important;
	}

	/deep/.u-textarea {
		padding-top: 0 !important;
		padding-bottom: 0 !important;
	}

	/deep/.u-button {
		border-radius: 12rpx !important;
	}

	/deep/.u-icon__icon {
		margin-right: 8rpx !important;
	}

	.title {
		line-height: 111rpx;
		text-align: center;
		font-size: 34rpx;
		font-family: PingFang SC;
		color: #5490FF;
	}

	.creative_results {
		padding: 0 20rpx 30rpx;
		margin-top: 20rpx;
		width: 696rpx;
		background: #FFFFFF;
		border-radius: 18rpx;
		box-sizing: border-box;

		.creative_results_btn {
			margin: auto;
			margin-top: 30rpx;
			width: 333rpx;
		}

		.results_keyWords {
			li {
				display: flex;
				justify-content: space-around;
				color: #5490FF;
			}

			// color: #5490FF;
			// font-size: 26rpx;
		}

	}

	.button {
		margin-top: 62rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;

		.button_left {
			width: 47%;
		}

		.button_right {
			position: relative;
			width: 47%;

			.right_other {
				display: flex;
				align-items: center;
				position: absolute;
				top: -21rpx;
				right: 0;
				width: 154rpx;
				height: 42rpx;
				background: #FF6600;
				border-radius: 21rpx 0rpx 0rpx 21rpx;

				text {
					line-height: 42rpx;
					margin-left: 18rpx;
					font-size: 18rpx;
					font-family: PingFang SC;
					color: #FFFFFF;
				}
			}
		}
	}

	.textarea {
		height: 722rpx;
	}

	.text {
		font-size: 19rpx;
		font-family: PingFang SC;
		color: #999999;
	}

	.aiWriting_box {
		padding: 0 27rpx;

		.content {
			width: 696rpx;
			height: 822rpx;
			border-radius: 18rpx;
			background-color: #fff;
		}

		.tools {
			height: 100rpx;
			padding: 0 27rpx;
			display: flex;
			justify-content: space-between;

			.tools_left {
				display: flex;
				justify-content: space-between;

				.left {
					display: flex;
					justify-content: space-between;
					align-items: center;
				}

				.right {
					margin-left: 40rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}

			.tools_right {
				display: flex;
				align-items: center;
			}
		}
	}
</style>
<style>
	page {
		background-color: #f4f4f4;
	}
</style>